package com.zyh.flink.day07;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.AssignerWithPeriodicWatermarks;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.watermark.Watermark;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import javax.annotation.Nullable;

public class EventTimeTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment environment = StreamExecutionEnvironment.getExecutionEnvironment();
        //并行度1
        environment.setParallelism(1);
        //设置使用EventTime
        environment.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);;

        SingleOutputStreamOperator<Tuple2<String, Long>> hadoop10 = environment.socketTextStream("hadoop10", 9998)
                .map(new MapFunction<String, Tuple2<String, Long>>() {
                    @Override
                    public Tuple2<String, Long> map(String s) throws Exception {
                        String[] ss = s.split("\\s+");
                        return Tuple2.of(ss[0], Long.valueOf(ss[1]));
                    }
                });
        OutputTag<Tuple2<String, Long>> tooLateTag = new OutputTag<>("tooLateData", Types.TUPLE(Types.STRING, Types.LONG));
        SingleOutputStreamOperator<String> result = hadoop10
                .assignTimestampsAndWatermarks(new MyAssignerWithPeriodicWatermarks(2000))
                .windowAll(TumblingEventTimeWindows.of(Time.seconds(5)))
                .allowedLateness(Time.seconds(2))//允许迟到的时间
                .sideOutputLateData(tooLateTag)//给太迟的数据打上标签
                .process(new ProcessAllWindowFunction<Tuple2<String, Long>, String, TimeWindow>() {
                    @Override
                    public void process(Context context, Iterable<Tuple2<String, Long>> iterable, Collector<String> collector) throws Exception {
                        StringBuilder sb = new StringBuilder();
                        for (Tuple2<String, Long> tuple2 : iterable) {
                            sb.append(tuple2.f0).append(" ");
                        }
                        TimeWindow window = context.window();
                        long start = window.getStart();
                        long end = window.getEnd();
                        System.out.print("start = " + start);
                        System.out.println(",end = " + end);
                        collector.collect(sb.toString());
                    }
                });
        result.print();
        result.getSideOutput(tooLateTag).print("tooLateData");
        environment.execute("Job");

    }
}
//自定义AssignerWithPeriodicWatermarks
class MyAssignerWithPeriodicWatermarks implements AssignerWithPeriodicWatermarks<Tuple2<String,Long>>{
    private long maxEventTime = 0;
    private long allowLateTime = 0;

    MyAssignerWithPeriodicWatermarks(long allowLateTime){
        this.allowLateTime = allowLateTime;
    }
    @Nullable
    @Override
    //抽取水位线的方法
    public Watermark getCurrentWatermark() {
        return new Watermark(maxEventTime-allowLateTime);
    }

    @Override
    //从元素中抽取时间戳的方法
    public long extractTimestamp(Tuple2<String, Long> value1, long value2) {
        Long currentTimestamp = value1.f1;
        if (currentTimestamp>maxEventTime){
            maxEventTime = currentTimestamp;//maxEventTime记录了当前最大的eventTime
        }
        System.out.println("当前的水位线 = " + (maxEventTime-allowLateTime));
        return currentTimestamp;
    }
}